package com.roubsite.cloud.common.db.multiple;

import com.roubsite.cloud.common.db.multiple.annotation.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {

	@Pointcut("@within(com.roubsite.cloud.common.db.multiple.annotation.DataSource) || @annotation(com.roubsite.cloud.common.db.multiple.annotation.DataSource)")
	public void pointCut() {

	}

	@Before("pointCut() && @annotation(dataSource)")
	public void doBefore(DataSource dataSource) {
		log.info("选择数据源---" + dataSource.value());
		DataSourceContextHolder.setDataSource(dataSource.value());
	}

	@After("pointCut()")
	public void doAfter() {
		DataSourceContextHolder.clear();
	}
}